package userAPI

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"go-gin-init/app/models/req"
	"go-gin-init/app/models/resp"
	"go-gin-init/app/service"
	"go-gin-init/common/ctx"
	"go-gin-init/common/result"
	"go-gin-init/common/tool"
)

// CurrentUser
// @Summary 当前用户
// @Description
// @Tags USER
// @Produce json
// @Security ApiKeyAuth
// @Param id path int true "请求参数"
// @Success 200 {object} result.JsonResult "{"code":200,"data":{},"msg":"SUCCESS"}"
// @Failure 400 {object} result.JsonResult "{"code":200,"data":{},"msg":""}"
// @Failure 500 {string} string "Internal Server Error"
// @Router /user/getCurrent/{id} [get]
func CurrentUser(c *gin.Context) {

	var u req.UserDeleteReq
	if err := c.ShouldBindUri(&u); err != nil {
		result.ParamErrorResult(c, err)
		return
	}

	if err := ctx.CheckPermissionCtx(u.Id, c); err != nil {
		result.JsonRsp(c, nil, err)
		return
	}

	userModel := service.NewDefaultUserModel()

	user, err := userModel.FindById(u.Id)
	if err != nil && err != service.ErrNotFound {
		result.JsonRsp(c, nil, err)
		return
	}

	var userResp resp.User
	if user != nil {
		copier.Copy(&userResp, user)
		// 对tags 字段处理
		userResp.Tags = tool.String2array(user.Tags)
	}

	result.JsonRsp(c, userResp, nil)

}
